Memory and CPU Profiling

Learn about memory and CPU profiling in Python.

We'll cover the following

Profiling a Python program means doing a dynamic analysis that measures the execution time of the program and everything that involves. That means measuring the time spent in each of its functions. This data gives you info about where your program is spending time, and what area might be worth optimizing.

This is a very interesting exercise. Many people focus on local optimization, such as determining, for example, which of the Python 2 functions range or xrange is going to be faster. It turns out that knowing which one is faster may never be an issue in your program, and that the time gained by using one of the functions above might not be worth the time you spend researching it or arguing about it with your colleague.

Trying to blindly optimize a program without measuring where it is actually spending its time is a useless exercise. Following your gut alone is not always sufficient.

cProfile#

There are many types of profiling, as there are many things that you can measure. Here we focus on CPU and memory utilization profiling, meaning the time spent by each function executing instructions or allocating memory.

Since its 2.5 version, Python provides a few built-in tools to help you in achieving that task. The standard one is cProfile and it is easy enough to use.

Click on the terminal below and enter the statement import cProfile. Then, enter cProfile.run('2 + 2'). All function calls and stats regarding the program execution will be displayed.

Terminal 1
Terminal

Click to Connect...

You can also use a script as an argument to cProfile.

In the following terminal, click Run and use the command python -m cProfile sample_script.py to view the cProfile of sample_script.py file.

/
sample_script.py
Terminal to perform cProfiling of sample_script.py

The resultant list indicates the number of times each function was called, and the time spent on its execution. You can use the -s option to sort by any fields, e.g. -s time sorts by internal time. Try using command python -m cProfile -s time sample_script.py in the above application.

Memory and CPU Profiling

Visualizing the call tree#

While this is handy, it is a bit rough to use and parse. However, if you have coded in C, you probably already know about the fantastic Valgrind tool, that, among other things, can provide profiling data for C programs. The data that it provides can then be visualized by another great tool called KCacheGrind.

You will be happy to know that the profiling information generated by cProfile can easily be converted to a call tree that can be read by KCacheGrind. The cProfile module has a -o option that allows you to save the profiling data, and pyprof2calltree can convert from one format to the other.

In the below terminal, click on the Run button. If KcacheGrind does not show up automatically after a few seconds in the Output tab, click on the URL provided.

/
sample_script.py
Terminal to run pyprof2calltree

After running the app, click on the “Call Graph” tab at the bottom of the right window in the KCacheGrind app to see the call graph of the script.

This provides a lot of information that will allow you to determine what part of your program might be consuming too many resources.

There is also another handy tool for visualizing profiling information which is called RunSnakeRun. It is simple to install using pip install runsnakerun.

Note: Unfortunately, it seems that for now RunSnakeRun only still works with Python 2.

Then you can use it without much difficulty:

Using runsnakerun
RunSnake example
RunSnake example

RunSnake offers the same kind of functionality as KCacheGrind, though it has a little less choice regarding what is displayed. However, it feels a little more interactive, and the navigation through the profiling information might be more accessible.

If only one of them is available on your platform, that is good enough. However, if you can, try and use both because they might complement each other well until you get used to using one or the other.

Introduction to performance optimization

Profiling Strategy and a Case